package cn.lijiguo.concurrency.part1.ch03;

import java.util.Arrays;

/**
 * CreateThread2
 * 演示 Thread 类的构造方法 2
 * @author lijiguo
 * @date 2019/12/12 16:54
 */
public class CreateThread2 {

    public static void main(String[] args) {
        // 创建线程的时候，如果不指定 ThreadGroupName 默认是 父线程的ThreadGroup 作为子线程的 ThreadGroup
        // 此时父线程和子线程在同一个 ThreadGroup 中
        Thread t1 = new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        t1.start();
//        System.out.println("t1 ThreadGroupName: " + t1.getThreadGroup().getName());
//        System.out.println("ThreadName: " + Thread.currentThread().getName());
//        System.out.println("main ThreadGroupName: " + Thread.currentThread().getThreadGroup().getName());

        ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
        int count = threadGroup.activeCount();
        System.out.println(count);
        Thread[] threads = new Thread[count];
        threadGroup.enumerate(threads);
        Arrays.asList(threads).forEach(System.out::println);
    }
}
